﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webchat.Models;
using System.Web.Security;

namespace Webchat.Controllers
{
    public class ChannelController : Controller
    {

        private ChannelRepository channelRep;

        public ChannelController()
        {
            channelRep = new ChannelRepository();
        }
        //
        // GET: /Channel/

        public ActionResult Index()
        {
            List<Chatroom> channels = channelRep.showAllChatRooms().ToList();
            return View(channels);
        }

        //
        // GET: /Channel/Room/name
        [Authorize]
        public ActionResult Room(string id)
        {
            Chatroom channel = channelRep.showChatroom(id);
            if (channel == null)
                return View("Notfound");

            return View(channel);
        }

        //
        // GET: /Channel/Create

        public ActionResult Create()
        {
            return View(new Chatroom());
        } 

        //
        // POST: /Channel/Create

        [HttpPost]
        [Authorize]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Chatroom room = new Chatroom();
                room.Date = System.DateTime.Now;
                room.OwnerId = channelRep.getId(User.Identity.Name);
              //  SelectList users = new SelectList(channelRep.showAllUsers(), "UserName", "Name");
              //  ViewData["users"] = users;
                UpdateModel(room);
                channelRep.addChannel(room);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Channel/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Channel/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Channel/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Channel/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
